package com.jiajunsong.blog.controller;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jiajunsong.blog.domain.Blog;
import com.jiajunsong.blog.domain.Catalog;
import com.jiajunsong.blog.domain.Result;
import com.jiajunsong.blog.domain.User;
import com.jiajunsong.blog.repository.BlogRepository;
import com.jiajunsong.blog.repository.CatalogRepository;
import com.jiajunsong.blog.repository.UserRepository;

@RestController
public class BlogController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private BlogRepository blogRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private CatalogRepository catalogRepository;

	@GetMapping("/blogs")
	public Page<Blog> blogs(Pageable pageable) {
		Page<Blog> page = blogRepository.findAll(pageable);
		return page;
	}

	@GetMapping("/blog")
	public Result blog(@RequestParam(required = true) Long id) {
		logger.info("id:" + id);
		// Blog blog = blogRepository.getOne(id);
		Optional<Blog> optionalBlog = blogRepository.findById(id);
		if (optionalBlog.isPresent()) {
			Blog blog = optionalBlog.get();
			blog.setReadSize(blog.getReadSize() + 1);
			blogRepository.save(blog);
			return new Result("", blog);
		} else {
			return new Result("对应的ID没有数据");
		}
	}

	@DeleteMapping("/blog")
	public Result deleteBlog(@RequestParam(required = true) Long id, Authentication authentication) {
		if (authentication == null) {
			return new Result("未登录");
		}
		Optional<Blog> blog = blogRepository.findById(id);
		if (blog.isPresent()) {
			String blogUserName = blog.get().getUser().getUsername();
			UserDetails loginedInUser = (UserDetails) (authentication.getPrincipal());// 也可以直接转成User，直接比较ID值
			String loginedInUserName = loginedInUser.getUsername();
			if (!blogUserName.equals(loginedInUserName)) {
				return new Result("只能删除自己的博客");

			} else {
				blogRepository.deleteById(id);
				return new Result("");
			}
		} else {
			return new Result("根据此ID没有找到博客");
		}
	}

	@PostMapping("/blog")
	public Result postBlog(@RequestBody Blog blog, Authentication authentication) {
		if (authentication == null) {
			return new Result("未登录");
		}
		User user = (User) authentication.getPrincipal();
		
		
		Catalog catalog = blog.getCatalog();
		if (catalog == null || catalog.getId() == null) {
			return new Result("必须指定分类");
		} else {
			Long catalogId = catalog.getId();
			Optional<Catalog> optionalCatalog = catalogRepository.findById(catalogId);
			if (optionalCatalog.isPresent()) {
				Catalog catalog_q = optionalCatalog.get();
				if (catalog_q.getUser().getId().equals(user.getId())) {
					blog.setCatalog(catalog_q);
				} else {
					return new Result("指定了分类，分类也存在，但是指定的分类不是当前登录用户的，你不能把blog的分类设定为别人的分类");
				}
			} else {
				return new Result("虽然指定里分类，但是指定的分类根本不存在");
			}
		}
		if (blog.getId() != null) {
			Optional<Blog> optionalBlog = blogRepository.findById(blog.getId());
			if (optionalBlog.isPresent()) {
				Blog blog_q = optionalBlog.get();
				if (!blog_q.getUser().getId().equals(user.getId())) {
					return new Result("根据blogId查找到的blog并不属于当前登录用户，你只能更改你自己的blog，不能更改别人的blog");
				}
			}
		}
		blog.setUser(user);
		blogRepository.save(blog);
		return new Result("",blog);
	}

	@GetMapping("/findblogbyuserid")
	public Result findblogbyuserid(@RequestParam(required = true) Long userId) {
		Optional<User> optionalUser = userRepository.findById(userId);
		if (optionalUser.isPresent()) {
			User user = optionalUser.get();
			List<Blog> blogs = blogRepository.findByUser(user);
			return new Result("", blogs);
		} else {
			return new Result("根据指定的userId找不到User");
		}
	}

}
